﻿CREATE PROCEDURE s_Accurint_InstantID
	@BorrID int = NULL,
	@LoanID int = NULL,
	@Pos tinyint = 0
AS

IF ISNULL(@BorrID,0) = 0 SELECT @BorrID = BorrowerID FROM t_Loan_Borrower WHERE LoanID = @LoanID AND Pos = @Pos
IF ISNULL(@BorrID,0) = 0 BEGIN
	RAISERROR('Borrower is not provided',16,1)
	RETURN
END

DECLARE @FN sysname,@LN sysname,@SSN sysname,@Street sysname,@City sysname,@State sysname,@Zip sysname,@DOB sysname,@Phone sysname,@DL sysname
DECLARE @Score int,@HTML nvarchar(max)

SELECT @FN=dbo.Trim(FirstName),@LN=dbo.Trim(LastName),@SSN=ISNULL(SSN,''),@Phone=ISNULL(PhoneHome,''),
			 @DOB=ISNULL(DOB,''),@Street=ISNULL(Street,''),@City=ISNULL(CityName,''),@Zip=ISNULL(Zip,''),@State=ISNULL(StateCode,''),@DL=''
FROM         v_Accurint
WHERE     (BorrowerID = @BorrID)

IF ISNULL(@FN,'') = '' OR ISNULL(@LN,'') = '' BEGIN
	RAISERROR('Must provide First and Last Name.',16,1)
	RETURN
END

EXEC @Score = InstantID 'abs-dev-09',@FN,@LN,@SSN,@Street,@City,@State,@Zip,@DOB,@Phone,@DL,@HTML OUTPUT

UPDATE t_Borrower SET InstantIDScore = @Score,InstantIDHtml = @HTML WHERE ID = @BorrID

RETURN @Score

